package edu.caltech.android.eew;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.location.Location;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/**
 * Based on http://stackoverflow.com/questions/9065597/draw-circle-with-transparency-in-mapview 
 * @author mfaulk
 *
 */
public class DiscOverlay extends Overlay {
	private static final String TAG = "DiscOverlay";
	
	private int mRadius;
	private GeoPoint mCenter;
	Point point = new Point();
	Paint circle = new Paint(Paint.ANTI_ALIAS_FLAG);
	private long systemTime= -1 ;
	private Context mContext;

	/**
	 * 
	 * @param point center of disc
	 * @param radiusMeters radius in meters.
	 */
	public DiscOverlay(GeoPoint discCenter, int radiusMeters, Context context) {
	    this.mCenter = discCenter;
	    this.mRadius = radiusMeters;
	    this.mContext = context;
	}

	public void setRadius(int radiusMeters) {
		this.mRadius = radiusMeters;
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {

	     // Transfrom geoposition to Point on canvas
	     Projection projection = mapView.getProjection();
	     projection.toPixels(mCenter, point);

	     // the circle to mark the spot
	     //circle.setColor(Color.parseColor("#88ff0000"));
	     circle.setColor(Color.CYAN);
	     circle.setAlpha(64); 

	     int radiusInPixels = metersToRadius(mRadius, mapView,
	     (double) mCenter.getLatitudeE6() / 1000000);

	     canvas.drawCircle(point.x, point.y, radiusInPixels, circle);       

	}



	public int metersToRadius(float meters, MapView map, double latitude) {
	    return (int) (map.getProjection().metersToEquatorPixels(meters) * (1 / Math
	            .cos(Math.toRadians(latitude))));
	}

	// See http://mobiforge.com/developing/story/using-google-maps-android for a good example.
//	@Override
//	public boolean onTouchEvent(MotionEvent event, MapView mapView) {
//	    switch (event.getAction()) {
//	    case MotionEvent.ACTION_DOWN:
//	        if ((System.currentTimeMillis() - systemTime) < 250) {
//	            mapView.getController().zoomIn();
//	        }
//	        systemTime = System.currentTimeMillis();
//	        break;
//	    }
//	    return false;
//	}

	@Override
	public boolean onTap(GeoPoint p, MapView mapView) {
		boolean inside = false;
		if(inDisc(p)) {
//			AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
//			dialog.setTitle("DiscOverlay");
//			dialog.setMessage("in the disc?");
//			dialog.show();
//			inside = true;	
		} 
		return inside;
	}
	
	private boolean inDisc(GeoPoint p) {
		float[] results = new float[1];
		Location.distanceBetween(p.getLatitudeE6(), p.getLongitudeE6(), mCenter.getLatitudeE6(), mCenter.getLongitudeE6(), results);
		double distanceInMeters = results[0] / 1000;
		Log.d(TAG, "Distance is " + Double.toString(distanceInMeters) + " meters.");
		return (distanceInMeters < 0.8* mRadius);
	}
	

 
}
